// Copyright (c) 2015, Microsoft Corporation
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
// SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
// IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
//
#include "pch.h"
#include "BridgeUtils.h"
#include "AdapterValue.h"

using namespace BridgeRT;
using namespace Platform;
using namespace Platform::Collections;
using namespace Windows::Foundation;
using namespace Windows::Foundation::Collections;
using namespace std;

using namespace BridgeRT;

namespace AdapterLib
{
    AdapterValue::AdapterValue(
        String^ ObjectName,
        Object^ DefaultData // = nullptr
        )
        : m_name(ObjectName)
        , m_data(DefaultData)
    {
    }

    uint32 AdapterValue::Set(IAdapterValue^ Other)
    {
        this->m_name = Other->Name;
        this->m_data = Other->Data;

        return ERROR_SUCCESS;
    }

    bool AdapterValue::operator==(const OCRepPayloadValue& val)
    {
        bool bRet = false;
        auto pv = dynamic_cast<IPropertyValue^>(m_data);

        if (pv)
        {
            switch (val.type)
            {
            case OCREP_PROP_INT:
            {
                bRet = (pv->Type == PropertyType::Int64) && (pv->GetInt64() == val.i);
                break;
            }
            case OCREP_PROP_DOUBLE:
            {
                bRet = (pv->Type == PropertyType::Double) && (pv->GetDouble() == val.d);
                break;
            }
            case OCREP_PROP_BOOL:
            {
                bRet = (pv->Type == PropertyType::Boolean) && (pv->GetBoolean() == val.b);
                break;
            }
            case OCREP_PROP_STRING:
            {
                bRet = (pv->Type == PropertyType::String) && (ConvertTo<string>(pv->GetString()) == val.str);
                break;
            }
            case OCREP_PROP_OBJECT:
            {
                //TODO
                break;
            }
            case OCREP_PROP_ARRAY:
            {
                //TODO
                break;
            }
            default:
            {
                break;
            }

            }
        }
        return bRet;
    }

    bool AdapterValue::operator!=(const OCRepPayloadValue& val)
    {
        return !(operator==(val));
    }

}   //namespace AdapterLib